import requests
from bs4 import BeautifulSoup
import datetime
import time
import json


class BaiShi:

    def __init__(self, start_pro, start_city, start_dis, des_pro, des_city, des_dis):
        # print '-'.join([start_pro, start_city, start_dis])
        # print '-'.join([des_pro, des_city, des_dis])
        # print str(int(time.mktime(datetime.datetime.now().timetuple()))) + '000'
        self.data = {
            "starCity": '-'.join([start_pro, start_city, start_dis]),
            "endCity": '-'.join([des_pro, des_city, des_dis]),
            "timestamp": str(int(time.mktime(datetime.datetime.now().timetuple()))) + '000',
            "weight": 1
        }

        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
        }

    def get_price(self):
        self.data["weight"] = 1
        r = requests.post('http://800bestex.bm8.com.cn/ajax.php', data=self.data, headers=self.headers)
        res_first = json.loads(json.loads(r.text))
        first_price = int(res_first['Data']['Price'])
        self.data["weight"] = 2
        r = requests.post('http://800bestex.bm8.com.cn/ajax.php', data=self.data, headers=self.headers)
        res_second = json.loads(json.loads(r.text))
        second_price = int(res_second['Data']['Price'])
        return [first_price, second_price-first_price]
